<template>
  <el-dialog title="物资查询" :visible.sync="localVisible" @close="closeDialog">
    <el-form v-if="showquery" :model="form" label-width="120px" class="query-form">
      <el-form-item label="物资名称">
        <el-input v-model="form.material_name"></el-input>
      </el-form-item>
      <el-form-item label="物资编号">
        <el-input v-model="form.material_code"></el-input>
      </el-form-item>
      <!-- 其他查询条件 -->
    </el-form>
    <el-table v-if="showquerylist" :data="querys" @row-dblclick="onRowDblClick" class="query-table">
      <el-table-column prop="material_name" label="设备名称" />
      <el-table-column prop="supplier" label="供应商" />
      <el-table-column prop="material_property" label="设备属性" />
      <el-table-column prop="material_type" label="设备类型" />
    </el-table>
    <el-form v-if="showinfo" :model="material" label-width="120px" class="material-info-form" ref="material">
      <el-form-item label="设备名称">
        <el-input v-model="material.material_name" disabled></el-input>
      </el-form-item>
      <el-form-item label="设备编号">
        <el-input v-model="material.material_code" disabled></el-input>
      </el-form-item>
      <el-form-item label="设备属性">
        <el-input v-model="material.material_property" disabled></el-input>
      </el-form-item>
      <el-form-item label="设备状态">
        <el-input v-model="material.material_status" disabled></el-input>
      </el-form-item>
      <el-form-item label="设备类型">
        <el-input v-model="material.material_type" disabled></el-input>
      </el-form-item>
      <el-form-item label="设备供应商">
        <el-input v-model="material.supplier" disabled></el-input>
      </el-form-item>
      <el-form-item label="入库时间">
        <el-input v-model="material.storage_date" disabled></el-input>
      </el-form-item>
      <el-form-item label="领货人">
        <el-input v-model="material.dispatch_person" disabled></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { queryMaterials } from '@/api/material';

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        material_name: '',
        material_code: ''
        // 其他查询条件
      },
      localVisible: this.visible,
      showquerylist: false,
      querys: [],
      material: {
        material_name: '',
        supplier: '',
        material_property: '',
        material_type: '',
        material_status: '',
        material_code: '',
        purchase_date: '',
        customer: '',
        storage_date: '',
        storage_person: '',
        dispatch_person: '',
        device_code: '',
      },
      showinfo: false,
      showquery: true
    };
  },
  watch: {
    visible(val) {
      this.localVisible = val;
    },
    localVisible(val) {
      this.$emit('update:visible', val);
    }
  },
  methods: {
    onRowDblClick(row) {
      this.material = { ...row }
      this.showquerylist = false;
      this.showinfo = true;
      this.showquery = false;
    },
    closeDialog() {
      this.localVisible = false;
      this.showquerylist = false;
    },
    async handleSubmit() {
      try {
        const response = await queryMaterials(this.form);
        this.querys = response.data;
        this.showquerylist = true;
      } catch (error) {
        console.log(error)
        this.$message.error('查询失败');
      }
    }
  }
};
</script>

<style scoped>
.query-form {
  margin-bottom: 20px;
}
.query-table {
  margin-bottom: 20px;
}
.material-info-form {
  margin-top: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
